<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Platform extends Model
{
    protected  $table='platform';

    protected $fillable = [
        'user_id',
        'name',
        'logo',
        'contact_name',
        'contact_phone',
        'contact_email',
        'contact_addr',
        'contact_province_id',
        'contact_province_name',
        'contact_city_id',
        'contact_city_name',
        'contact_county_id',
        'contact_county_name',

        'prefix',

        'cert_title',
        'cert_platform',
        'cert_cachet',
        'cert_auth_platform',

        'address',
        'corporate_name',
        'corporate_id',
        'corporate_phone',
        'quality',
        'module_settings',
        'org_list',
        'platform_info',
        'platform_manual',
        'platform_service',
        'platform_service_tel',
    ];


    protected $casts = [
        'module_settings' => 'array',
        'quality' => 'array',
    ];

    protected $appends = array('address', 'platform_url');

    public function getOrgListAttribute() {
        if (isset($this->attributes['org_list'])) {
            try {
                $this->attributes['org_list'] = json_decode($this->attributes['org_list']);
            }catch (\Exception $e) {

            }


            return $this->attributes['org_list'];
        }

        return [];
    }

    public function setOrgListAttribute($value) {
        if (!empty($value)) {
            $value = json_encode($value);
        }

        $this->attributes['org_list'] = $value;
    }

    public function getPlatformUrlAttribute() {
        if (empty($this->prefix)) {
            return null;
        }
        return 'http://'.$this->prefix.env('PLATFORM_BASE_HOST', '.platform.alpha.flashdiet.cn');
    }

    public function getQualityAttribute() {
        if (isset($this->attributes['quality'])) {
            try {
                $this->attributes['quality'] = json_decode($this->attributes['quality']);
            }catch (\Exception $e) {

            }

            if (is_array($this->attributes['quality'])) {
                return join(',', $this->attributes['quality']);
            }

            return $this->attributes['quality'];
        }

        return null;
    }

    public function setQualityAttribute($value) {
        if (!empty($value)) {
            $value = explode(',', $value);
            $value = json_encode($value);
        }

        $this->attributes['quality'] = $value;
    }

    public function setAddressAttribute($value) {
        $addressId = $value['id'];
        $this->attributes['contact_province_id'] = intval(substr($addressId, 0, 2).'0000');
        $this->attributes['contact_province_name'] = $value['province'];
        $this->attributes['contact_city_id'] = intval(substr($addressId, 0, 4).'00');
        $this->attributes['contact_city_name'] = $value['city'];
        $this->attributes['contact_county_id'] = intval(substr($addressId, 0, 6));
        $this->attributes['contact_county_name'] = $value['district'];
        $this->attributes['contact_addr'] = isset($value['address']) ? $value['address'] : '';
    }

    public function getAddressAttribute() {
        if (!isset($this->attributes['contact_county_id'])) {
            return null;
        }

        return [
            'id' => $this->attributes['contact_county_id'],
            'province' => $this->attributes['contact_province_name'],
            'city' => $this->attributes['contact_city_name'],
            'district' => $this->attributes['contact_county_name'],
            'address' => $this->attributes['contact_addr'],
        ];
    }

    public function getScoreConf()
    {
        $config = $this->score_settings;

        $config['register'] = isset($config['register'])? $config['register']:10;
        $config['certification'] = isset($config['certification'])? $config['certification']:10;
        $config['reward_time'] = isset($config['reward_time'])? $config['reward_time']:10;
        $config['project_hire'] = isset($config['project_hire'])? $config['project_hire']:10;
        unset($config['project_hire']);
        return $config;
    }

}
